package cn.quevo.core.entity;

import java.util.ArrayList;
import java.util.List;

import cn.quevo.core.enums.EnumsInterface;
import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CustomException;
import cn.quevo.core.lang.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 分页数据对象
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 14:30:12
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class PageResult<T> extends ResultEntity {

    /**
     *
     */
    private static final long serialVersionUID = -6806748131866292250L;
    /**
     * 总记录条数
     */
    private Long total = 0L;
    /**
     * 记录集
     */
    private List<T> rows = new ArrayList<>();
    /**
     * 是否有更多
     */
    private String more;
    /**
     * 当前页数
     */
    private Integer curPage = 1;
    /**
     * 总共页数
     */
    private Integer countPage = 0;
    /**
     * 每页记录条数
     */
    private Integer pageSize = 0;
    /**
     * 从{}条记录开始显示
     */
    private Long first = 0L;
    /**
     * 到{}记录结束
     */
    private Long last = 0L;
    /**
     * 实际分页记录条数
     */
    private Integer size = 0;
    /**
     * 每页最大记录条数
     */
    public static final Integer maxSize = 500;

    public PageResult() {
        this(ResultStatusEnum.SUCCESS);
    }

    public PageResult(EnumsInterface enums) {
        super(enums);
    }

    public PageResult(CustomException exception) {
        super(exception);
    }

    public PageResult(List<T> rows) {
        super(ResultStatusEnum.SUCCESS);
        if (rows != null) {
            this.rows = rows;
            this.size = rows.size();
            this.total = Long.valueOf(size);
            this.last = this.total;
        }
    }

    public Integer getSize() {
        return StringUtils.isEmpty(this.rows) ? 0 : this.rows.size();
    }

    @Override
    public String toString() {
        if (super.getStatus() != 200) {
            return super.toString();
        }
        StringBuilder msg = new StringBuilder();
        msg.append("共 ").append(this.countPage == null ? 0 : this.countPage).append("页,").append(this.total == null ? 0 : this.total).append("条记录\t\n");
        msg.append("当前第").append(this.curPage == null ? 1 : this.curPage).append("页,加载").append(this.size == null ? 0 : this.size).append("条记录");
        return msg.toString();
    }
}
